package com.ss.android.article.base.feature.detail2.a.a;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.model.DownloadAppAd;
import com.ss.android.article.browser.R;
import com.ss.android.article.common.NightModeAsyncImageView;
import com.ss.android.common.download.DownloadAdapterProxy;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.download.DownloadInfoChangeListener;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.newmedia.util.AppUtil;

public final class AppAdViewHolder {
    final class a$a implements DownloadInfoChangeListener {
        public final AppAdViewHolder b;
        long a;

        a$a(AppAdViewHolder arg1) {
            this.b = arg1;
        }

        public final void setDownloadId(long id) {
            this.a = id;
        }

        public final void onDownloadProgressChanged(DownloadShortInfo arg10, int arg11, long arg12, long arg14, long arg16) {
            this.b.mItemView.post(new d(this, arg10, arg11, arg14, arg12));
        }
    }

    final class QueryDownloadInfoTask extends AsyncTask {
        private final AppAdViewHolder a;

        QueryDownloadInfoTask(AppAdViewHolder arg1) {
            this.a = arg1;
        }

        protected final Object doInBackground(Object[] arg4) {
            String[] strArr = (String[]) arg4;
            DownloadShortInfo v0;
            if (arg4 != null) {
                if (arg4.length > 0 && (TextUtils.isEmpty(strArr[0]))) {
                    return null;
                }

                if (this.a.j == null) {
                    return null;
                }

                v0 = DownloadAdapterProxy.inst().queryDownloadInfo(this.a.j, strArr[0]);
            } else {
                return null;
            }

            return v0;
        }

        protected final void onPostExecute(Object arg10) {
            int v0_4;
            Resources v2;
            TextView v1;
            String v0_3 = null;
            String v7_1 = null;
            super.onPostExecute(arg10);
            if (!this.isCancelled()) {
                boolean v0 = AppUtil.a(this.a.j, this.a.l.version, this.a.l.pkg);
                try {
                    this.a.m = ((DownloadShortInfo) arg10);
                    if ((AppData.getInstance().allowInsideDownloadManager()) && arg10 != null && ((DownloadShortInfo) arg10).id > -1 && !DownloadAdapterProxy.inst().isDownloadSuccessAndFileNotExist(this.a.j, ((DownloadShortInfo) arg10)) && !v0) {
                        if (arg10 != null) {
                            Logger.d("AppAdViewHolder QueryDownloadInfoTask", "result.id = " + ((DownloadShortInfo) arg10).id + " appName = " + this.a.l.appName);
                            DownloadAdapterProxy.inst().registerDownloadListener(this.a.j, Long.valueOf(((DownloadShortInfo) arg10).id), this.a.n, String.valueOf(this.a.l.id), 2, this.a.l.logExtra);
                        }

                        this.a.b.setVisibility(View.GONE);
                        this.a.c.setVisibility(View.GONE);
                        this.a.e.setVisibility(View.VISIBLE);
                        this.a.f.setVisibility(View.GONE);
                        this.a.h.setVisibility(View.VISIBLE);
                        switch (((DownloadShortInfo) arg10).b) {
                            case 1:
                            case 2: {
                                String v8 = android.support.design.a.a(((DownloadShortInfo) arg10).d) + "/" + android.support.design.a.a(((DownloadShortInfo) arg10).c);
                                v7_1 = this.a.k.getString(com.ss.android.article.browser.R.string.kf);
                                UIUtils.setMaxLines(this.a.d, R.drawable.af);
                                this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.cz));
                                DownloadAdapterProxy.inst().registerDownloadListener(this.a.j, Long.valueOf(((DownloadShortInfo) arg10).id), this.a.n, String.valueOf(this.a.l.id), 2, this.a.l.logExtra);
                                v0_3 = v7_1;
                                v7_1 = v8;
                                break;
                            }
                            case 4: {
                                v7_1 = android.support.design.a.a(((DownloadShortInfo) arg10).d) + "/" + android.support.design.a.a(((DownloadShortInfo) arg10).c);
                                v0_3 = this.a.k.getString(com.ss.android.article.browser.R.string.ki);
                                UIUtils.setMaxLines(this.a.d, R.drawable.ag);
                                this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.dc));
                                break;
                            }
                            case 8: {
                                if (ToolUtils.d(this.a.j, ((DownloadShortInfo) arg10).fileName)) {
                                    v0_3 = this.a.k.getString(com.ss.android.article.browser.R.string.ke);
                                } else {
                                    v0_3 = this.a.k.getString(com.ss.android.article.browser.R.string.k9);
                                }

                                label_229:
                                this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.cx));
                                UIUtils.setMaxLines(this.a.d, R.drawable.ae);
                                this.a.f.setVisibility(View.VISIBLE);
                                this.a.h.setVisibility(View.GONE);
                                v7_1 = android.support.design.a.a(((DownloadShortInfo) arg10).d);
                                this.a.e.setVisibility(View.GONE);
                                break;
                            }
                            case 16: {
                                this.a.f.setVisibility(View.VISIBLE);
                                this.a.h.setVisibility(View.GONE);
                                v7_1 = android.support.design.a.a(((DownloadShortInfo) arg10).c);
                                v0_3 = this.a.k.getString(com.ss.android.article.browser.R.string.kh);
                                this.a.e.setVisibility(View.GONE);
                                UIUtils.setMaxLines(this.a.d, R.drawable.ag);
                                this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.d1));
                                break;
                            }
                            default: {
                                v0_3 = null;
                                break;
                            }
                        }
                    } else {
                        label_265:
                        if (this.a.m != null) {
                            DownloadAdapterProxy.inst().unregisterDownloadListener(this.a.j, Long.valueOf(this.a.m.id), this.a.n);
                        }
                        this.a.m = null;
                        if (android.support.design.a.isTextEmpty(this.a.l.btnTxt)) {
                            v1 = this.a.d;
                            v2 = this.a.k;
                            v0_4 = v0 ? R.string.kj : R.string.ka;
                            v1.setText(v2.getString(v0_4));
                        } else {
                            this.a.d.setText(this.a.l.btnTxt);
                        }

                        this.a.d.setBackgroundResource(com.ss.android.article.browser.R.drawable.ad_action_btn_begin_bg);
                        this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.ad_action_btn_begin_text_color));
                        this.a.e.setVisibility(View.GONE);
                        this.a.h.setVisibility(View.GONE);
                        this.a.f.setVisibility(View.GONE);
                        this.a.c();
                        return;
                    }
                    if (((DownloadShortInfo) arg10).c > 0) {
                        this.a.e.setProgress(((int) (((DownloadShortInfo) arg10).d * 100 / ((DownloadShortInfo) arg10).c)));
                    } else {
                        this.a.e.setProgress(0);
                    }

                    if (this.a.h.getVisibility() == View.VISIBLE) {
                        this.a.i.setText(v7_1);
                        this.a.d.setText(v0_3);
                    }

                    if (this.a.f.getVisibility() != View.VISIBLE) {
                        return;
                    }

                    this.a.g.setText(v7_1);
                    this.a.d.setText(v0_3);
                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }
    }

    public View mItemView;
    public RatingBar b;
    public TextView c;
    public TextView d;
    public ProgressBar e;
    public View f;
    public TextView g;
    public View h;
    public TextView i;
    protected Context j;
    protected final Resources k;
    public DownloadAppAd l;
    public DownloadShortInfo m;
    public final a$a n;
    private LinearLayout o;
    private NightModeAsyncImageView p;
    private TextView q;
    private TextView r;
    private View s;
    private boolean t;
    private int u;
    private QueryDownloadInfoTask v;
    private View.OnClickListener w;
    private View.OnClickListener x;

    public AppAdViewHolder(Context arg5) {

        this.t = false;
        this.n = new a$a(this);
        this.w = new com.ss.android.article.base.feature.detail2.a.a.b(this);
        this.x = new com.ss.android.article.base.feature.detail2.a.a.c(this);
        this.j = arg5;
        Spipe.inst();
        AppData.getInstance();
        this.k = this.j.getResources();
        TTUtils.getNightModeColorFilter();
        this.u = ((int) ((((double) this.k.getDisplayMetrics().widthPixels)) * 0.62));
    }

    public final void a() {
        int v8 = R.color.appad_v18_download_text;
        int v7 = R.color.dc;
        int v6 = 2;
        this.t = AppData.isNightMode();
        UIUtils.setBackgroundResource(this.mItemView, R.drawable.bg_detail_action);
        Drawable v0 = this.b.getProgressDrawable();
        if (this.t) {
            ((LayerDrawable) v0).getDrawable(0).setColorFilter(this.k.getColor(com.ss.android.article.browser.R.color.e8), PorterDuff.Mode.SRC_IN);
            ((LayerDrawable) v0).getDrawable(1).setColorFilter(this.k.getColor(com.ss.android.article.browser.R.color.e9), PorterDuff.Mode.SRC_IN);
            ((LayerDrawable) v0).getDrawable(v6).setColorFilter(this.k.getColor(com.ss.android.article.browser.R.color.e9), PorterDuff.Mode.SRC_IN);
        } else {
            ((LayerDrawable) v0).getDrawable(0).clearColorFilter();
            ((LayerDrawable) v0).getDrawable(1).clearColorFilter();
            ((LayerDrawable) v0).getDrawable(v6).clearColorFilter();
        }

        this.c.setTextColor(this.k.getColor(com.ss.android.article.browser.R.color.appad_v18_hot_text));
        this.c.setCompoundDrawablesWithIntrinsicBounds(com.ss.android.article.browser.R.drawable.hoticon_textpage_ad, 0, 0, 0);
        this.i.setTextColor(this.k.getColor(v8));
        this.g.setTextColor(this.k.getColor(v8));
        this.e.setProgressDrawable(this.k.getDrawable(com.ss.android.article.browser.R.drawable.ad_download_progress_bar_horizontal));
        this.e.getProgressDrawable().setBounds(this.e.getProgressDrawable().getBounds());
        if (this.m != null) {
            switch (this.m.b) {
                case 4: {
                    UIUtils.setMaxLines(this.d, R.drawable.af);
                    this.d.setTextColor(this.k.getColor(com.ss.android.article.browser.R.color.cz));
                    break;
                }
                case 8: {
                    if (ToolUtils.d(this.j, this.m.fileName)) {
                        UIUtils.setMaxLines(this.d, R.drawable.an);
                        this.d.setTextColor(this.k.getColor(com.ss.android.article.browser.R.color.d9));
                    } else {
                        UIUtils.setMaxLines(this.d, R.drawable.ap);
                        this.d.setTextColor(this.k.getColor(com.ss.android.article.browser.R.color.da));
                    }
                }
                case 1:
                case 2:
                case 16: {
                    break;
                }
            }

        } else {
            UIUtils.setMaxLines(this.d, R.drawable.am);
            this.d.setTextColor(this.k.getColorStateList(v7));
        }

        label_64:
        this.r.setTextColor(this.k.getColor(com.ss.android.article.browser.R.color.e2));
        this.s.setBackgroundResource(com.ss.android.article.browser.R.color.ssxinmian4);
    }

    public final void initView(View itemView) {
        this.mItemView = itemView;
        this.o = itemView.findViewById(com.ss.android.article.browser.R.id.appad_layout);
        this.p = itemView.findViewById(com.ss.android.article.browser.R.id.appicon);
        this.r = itemView.findViewById(com.ss.android.article.browser.R.id.appname);
        this.q = itemView.findViewById(com.ss.android.article.browser.R.id.appicon_lable_info);
        this.b = itemView.findViewById(com.ss.android.article.browser.R.id.star);
        this.c = itemView.findViewById(com.ss.android.article.browser.R.id.hot);
        this.d = itemView.findViewById(com.ss.android.article.browser.R.id.ad_action_btn);
        this.e = itemView.findViewById(com.ss.android.article.browser.R.id.download_progress);
        this.f = itemView.findViewById(com.ss.android.article.browser.R.id.download_success);
        this.g = itemView.findViewById(com.ss.android.article.browser.R.id.download_success_size);
        this.h = itemView.findViewById(com.ss.android.article.browser.R.id.download_text);
        this.i = itemView.findViewById(com.ss.android.article.browser.R.id.download_size);
        this.s = itemView.findViewById(com.ss.android.article.browser.R.id.appad_right_area);
        this.mItemView.setOnClickListener(this.w);
        this.d.setOnClickListener(this.x);
    }

    public final void a(DownloadAppAd arg4) {
        int v0 = 0;
        if (arg4 != null) {
            this.l = arg4;
            if (this.t != AppData.isNightMode()) {
                this.a();
            }

            if (this.l == null) {
                return;
            }

            DownloadAppAd v1 = this.l;
            if (v1 != null && v1.imageInfo != null && v1.imageInfo != null && v1.imageInfo.mWidth > 0) {
                v0 = this.u * v1.imageInfo.mHeight / v1.imageInfo.mWidth;
            }

            NightModeAsyncImageView v1_1 = this.p;
            ViewGroup.LayoutParams v2 = ((ImageView) v1_1).getLayoutParams();
            if (v2 != null) {
                if (v0 > 0) {
                    v2.height = v0;
                }

                ((ImageView) v1_1).setLayoutParams(v2);
            }

            if (this.l.imageInfo != null) {
                this.p.setImage(android.support.design.a.imageInfo2Image(this.l.imageInfo));
            }

            this.b();
        }
    }

    public final void b() {
        Drawable v2 = null;
        UIUtils.setVisibility(this.o, View.GONE);
        if (this.l != null) {
            UIUtils.setVisibility(this.o, 0);
            this.r.setText(this.l.appName);
            String v0 = this.l.appAdSourceLabel;
            if (!android.support.design.a.isTextEmpty(v0)) {
                this.q.setText(((CharSequence) v0));
            }

            this.d.setCompoundDrawables(v2, v2, v2, v2);
            this.d.setGravity(17);
            if (AppData.getInstance().allowInsideDownloadManager()) {
                if (this.m != null) {
                    Logger.d("AppAdViewHolder bindAppAd unregisterDownloadListener", "downloadShortInfo.id = " + this.m.id + " appName = " + this.l.appName);
                    DownloadAdapterProxy.inst().unregisterDownloadListener(this.j, Long.valueOf(this.m.id), this.n);
                    this.b.setVisibility(View.GONE);
                    this.c.setVisibility(View.GONE);
                } else {
                    this.c();
                }

                if (this.v != null && this.v.getStatus() != AsyncTask.Status.FINISHED) {
                    this.v.cancel(true);
                }

                this.v = new QueryDownloadInfoTask(this);
                com.bytedance.common.utility.b.a.a(this.v, new String[]{this.l.downloadUrl});
                return;
            }

            this.c();
            this.f.setVisibility(View.GONE);
            this.h.setVisibility(View.GONE);
            this.e.setVisibility(View.GONE);
            UIUtils.setMaxLines(this.d, R.drawable.am);
        }
    }

    final void c() {
        int v0 = 10;
        if (this.l.rate <= v0) {
            v0 = this.l.rate;
        }

        float v0_1 = (((float) v0)) / 2f;
        if (Logger.debug()) {
            Logger.d("ratingBar", "rating = " + v0_1 + " appName = " + this.l.appName);
        }

        if (!android.support.design.a.isTextEmpty(this.l.downloadCount)) {
            UIUtils.setVisibility(this.f, 0);
            UIUtils.setVisibility(this.g, 0);
            UIUtils.setVisibility(this.b, View.GONE);
            UIUtils.setVisibility(this.c, View.GONE);
            this.g.setText(this.l.downloadCount);
        } else {
            UIUtils.setVisibility(this.f, View.GONE);
            if (v0_1 < 0f) {
                this.b.setVisibility(View.GONE);
                this.c.setVisibility(View.VISIBLE);
            } else {
                this.b.setVisibility(View.VISIBLE);
                this.b.setRating(v0_1);
                this.c.setVisibility(View.GONE);
            }
        }
    }
}

